package org.example.everyday;

public class Lc393 {

    private static int[] masks = new int[]{128,64,32,16,8};

    public static void main(String[] args) {
        Lc393 lc393 = new Lc393();
        System.out.println(lc393.vliadUtf8(new int[]{192,130,1}));
    }

    private int getOIndex(int num){
        for (int i = 0; i < masks.length; i++) {
            if((num & masks[i]) == 0){
                return i;
            }
        }
        return -1;
    }

    public boolean vliadUtf8(int[] data){
        int len = data.length;
        for (int i = 0; i < len; i++) {
            int curr = data[i];
            int idx = getOIndex(curr);
            if(idx == 0){
                continue;
            }else if(idx > 1 && i + idx <= len){
                while (idx-- > 1){
                    if(getOIndex(data[++i]) != 1){
                        return false;
                    }
                }
            }else {
                return false;
            }
        }

        return true;
    }


}
